package com.ruoyi.pay.alipay;

import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.ijpay.core.enums.TradeType;
import com.ruoyi.pay.AbstractPayService;
import com.ruoyi.pay.config.AliPayProperties;
import com.ruoyi.pay.config.PayProperties;
import com.ruoyi.pay.model.PayNotifyResultModel;
import com.ruoyi.pay.model.PayRequestModel;
import com.ruoyi.pay.model.PayResultModel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service("aliPayService")
public class AliPayService extends AbstractPayService {

    private final PayProperties aliPayProperties;

    @PostConstruct
    @Override
    public void init() {
        aliPayProperties.getAli().forEach(payProperties -> {
            AliPayApiConfig wxConfig = AliPayApiConfig.builder()
                .setAppId(payProperties.getAliAppId())
                .setAppCertPath(payProperties.getAliAppCertPath())
                .setAliPayCertPath(payProperties.getAliPayCertPath())
                .setAliPayRootCertPath(payProperties.getAliPayRootCertPath())
                .setCharset("UTF-8")
                .setPrivateKey(payProperties.getAliPrivateKey())
                .setAliPayPublicKey(payProperties.getAliPayPublicKey())
                .setServiceUrl(payProperties.getServerUrl())
                .setSignType("RSA2")
                // 普通公钥方式
                .build();
            AliPayApiConfigKit.putApiConfig(wxConfig);
        });
    }

    @Override
    public PayResultModel pay(PayRequestModel payRequestModel) {
        PayResultModel resultModel = new PayResultModel();
        TradeType tradeType = payRequestModel.getTradeType();
        String body = payRequestModel.getBody();
        String subject = payRequestModel.getSubject();
        BigDecimal totalFee = payRequestModel.getTotalFee();
        String outTradeNo = payRequestModel.getOutTradeNo();
        String appId = payRequestModel.getAppId();
        String passBackParams = payRequestModel.getPassBackParams();
        // h5
        if (TradeType.MWEB == tradeType) {
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            model.setBody(body);
            model.setSubject(subject);
            model.setTotalAmount(totalFee.toString());
            model.setPassbackParams(passBackParams);
            model.setOutTradeNo(outTradeNo);
            model.setProductCode("QUICK_WAP_PAY");
            try {
                AliPayProperties payProperties = aliPayProperties.getByAliAppId(appId);
                String wapPayStr = AliPayApi.wapPayStr(model, payProperties.getAliRefundUrl(), payProperties.getAliNotifyUrl());
                resultModel.setSuccess(true);
                resultModel.addResultInfo("wapPayStr", wapPayStr);
                return resultModel;
            } catch (Exception e) {
                resultModel.setMsg(e.getMessage());
                resultModel.setSuccess(false);
                log.error("支付宝异常：", e);
            }
        }
        return resultModel;
    }

    @Override
    public PayNotifyResultModel payNotify(Map<String, String> params, String appId) {
        return null;
    }
}
